package com.easy.stock.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.easy.stock.domain.ConfigItem;

public class ConfigDao extends BaseDao {
	
	public void addConfig(ConfigItem item) {
		String query = "{app:\""+item.getApp()
				+"\", group:\""+item.getGroup()
				+"\", key:\"" +item.getKey()
				+"\"}, {date:#}";
		getCollection().update(query, item.getDate()).upsert().with(item);
	}
	
	public void removeOldConfig(String app, String group) {
		
		String query = "{app:\""+app
				+"\", group:\""+group+"\"}";
		getCollection().remove(query);
	}
	
	public List<ConfigItem> getConfigItemList(String app, String group) {
		
		String query = "{app:\""+app
				+"\", group:\""+group+"\"}";
		
		Iterable<ConfigItem> data = getCollection().find(query).as(ConfigItem.class);
		List<ConfigItem> dataList = new ArrayList<ConfigItem>();
		Iterator<ConfigItem> iter = data.iterator();
		while(iter.hasNext()) {
			dataList.add(iter.next());
		}
		return dataList;
	}
	
	public ConfigItem getConfig(String app, String group, String key) {
		String query = "{app:\""+app+"\", group:\""+group+"\", key:\""+key+"\"}";
		ConfigItem cfgItem = getCollection().findOne(query).as(ConfigItem.class);
		return cfgItem;
	}

}
